// ignore_for_file: file_names, must_be_immutable, constant_identifier_names, slash_for_doc_comments, avoid_unnecessary_containers, prefer_const_constructors, unnecessary_cast, sized_box_for_whitespace, prefer_function_declarations_over_variables, overridden_fields, annotate_overrides, prefer_const_literals_to_create_immutables, unnecessary_null_comparison, deprecated_member_use

import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mybase/base/tool/chain/FFedge.dart';
import 'package:mybase/base/tool/delay/FFDelay.dart';
import 'package:mybase/base/tool/empty/FFEmpty.dart';
import 'package:mybase/base/tool/globalMethod/FFListen.dart';
import 'package:mybase/base/tool/globalMethod/FFScreenValue.dart';
import 'package:mybase/base/tool/globalMethod/FFScript.dart';
import 'package:mybase/base/widget/layout/FFContainer.dart';
import 'package:mybase/base/widget/layout/FFLineContainer.dart';
import 'package:mybase/base/widget/layout/FFTextButton.dart';

class ScreenPrintManager {
  // 单例公开访问点
  factory ScreenPrintManager() => _sharedInstance();

  // 静态、同步、私有访问点
  static ScreenPrintManager _sharedInstance() {
    return _instance;
  }

  // 静态私有成员，没有初始化
  static final ScreenPrintManager _instance = ScreenPrintManager._();

  // 私有构造函数
  ScreenPrintManager._() {
    // 具体初始化代码
  }

  final ScrollController controller = ScrollController();
  bool enablePrint = true;

  late OverlayState overlayState;
  List<DebugEntity> list = [];
  bool isInited = false;
  bool isShow = true;

  final ValueNotifier<int> valueListenable = ValueNotifier(0);

  ///显示或者隐藏
  switchDisplay() {
    if (kReleaseMode) return;
    isShow = !isShow;
    refreshUI();
  }

  ///屏幕打印
  debugPrint(BuildContext? context,
      String message, {
        Color fontColor = Colors.green,
        int maxLine = 6,
        int second = 16,
        double fontSize = 9.0,
      }) async {
    if (kDebugMode) return;
    if (kReleaseMode) return;
    if (context == null) return;
    if (enablePrint) {
      DebugEntity debugEntity = DebugEntity(
        message: message,
        fontColor: fontColor,
        fontSize: fontSize,
        isShow: isShow,
        maxLine: maxLine,
      );

      list.add(debugEntity);

      if (isShow == true) {
        FFDelay delay = FFDelay(
            delay: Duration(seconds: second),
            onDelay: (FFDelay? delay) {
              if (delay != null) {
                debugEntity.isShow = false;
                refreshUI();
              }
            })
          ..start();
        debugEntity.delay = delay;
      }

      if (!isInited) {
        ///进行初始化
        isInited = true;
        OverlayEntry overlayEntry = OverlayEntry(
          builder: (context) {
            return IgnorePointer(
              ignoring: true,
              child: Container(
                width: globalScreenWidth(context),
                margin: EdgeInsets.only(top: globalScreenPaddingTop(context)),
                child: Material(
                  color: Colors.transparent,
                  child: Container(
                    alignment: Alignment(-1, -1),
                    child: globalValueListen(
                        valueListenable: valueListenable,
                        builder: (ctx, int count, Widget? child) {
                          return Opacity(
                            opacity: isShow ? 1 : 0,
                            child: FFContainer(
                              unconstrainedBox: true,
                              width: globalScreenWidth(context) * 0.75,
                              constraints: BoxConstraints(
                                  minHeight: 0,
                                  maxHeight: globalScreenHeight(context) * 0.9),
                              child: ListView.builder(
                                controller: controller,
                                itemBuilder: (ctx, index) {
                                  DebugEntity entity = list[index];
                                  return FFLineContainer(
                                    direction: Axis.horizontal,
                                    crossAxisAlignment:
                                    CrossAxisAlignment.start,
                                    hiddenKeepFrame: !(entity.isShow == true),
                                    children: [
                                      Container(
                                        width: 2,
                                        height: 10,
                                        color: entity.fontColor,
                                        margin: EdgeInsets.only(top: 2),
                                      ),
                                      Expanded(
                                        child: FFTextButton(
                                          margin: FFedge()
                                              .bottom(10)
                                              .left(8)
                                              .build(),
                                          text: "[$index]${entity.message}",
                                          maxLines: entity.maxLine,
                                          textStyle: TextStyle(
                                            fontSize: entity.fontSize,
                                            color: entity.fontColor,
                                          ),
                                        ),
                                      )
                                    ],
                                  );
                                },
                                itemCount: list.length,
                              ),
                            ),
                          );
                        }),
                  ),
                ),
              ),
            );
          },
        );

        overlayState = Overlay.of(context);
        overlayState.insert(overlayEntry);
      }

      ///触发更新
      refreshUI();

      await Future.delayed(Duration(milliseconds: 100));
      controller.animateTo(
        controller.position.maxScrollExtent,
        duration: const Duration(milliseconds: 100),
        curve: Curves.linear,
      );
    }
  }

  refreshUI() {
    valueListenable.value++;
  }
}

class DebugEntity {
  String? message;
  Color? fontColor;
  double? fontSize;
  bool? isShow;
  FFDelay? delay;
  int maxLine;

  DebugEntity({
    required this.message,
    required this.fontColor,
    required this.fontSize,
    this.isShow,
    this.delay,
    this.maxLine = 6,
  });
}
